package br.com.gaspar.entidade;

import gov.sefaz.utils.entidade.EntidadeBase;

import java.math.BigDecimal;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.validation.constraints.NotNull;

@Entity
@Table(name="tbl_pagamento_compra")

@NamedQueries({
	@NamedQuery(name="PagamentoCompra.buscarTodos", query="select obj from PagamentoCompra obj order by obj.data"),
	@NamedQuery(name="PagamentoCompra.buscarPorData", query="select obj from PagamentoCompra obj where obj.data =:p0 order by obj.data desc"),
	@NamedQuery(name="PagamentoCompra.buscarPorCompra", query="select obj from PagamentoCompra obj where obj.compra.id =:p0 order by obj.data desc"),
	@NamedQuery(name="PagamentoCompra.buscarTotalPorCompra", query="select SUM(obj.valor) from PagamentoCompra obj where obj.compra.id =:p0 group by obj.valor")
})
public class PagamentoCompra extends EntidadeBase {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	@Id
	@GeneratedValue(strategy=GenerationType.AUTO)
	private Long id;
	
	@Temporal(TemporalType.DATE)
	@Column(nullable=false)
	@NotNull(message="Obrigatório informar a data!")
	private Date data;
	
	@Column(nullable=false)
	@NotNull(message="Obrigatório informar o valor!")
	private BigDecimal valor;
	
	@ManyToOne(fetch=FetchType.EAGER)
	@JoinColumn(name="id_compra", nullable=false)
	@NotNull(message="Obrigatório relacionar a qual compra pertence o pagamento!")
	private Compra compra;
	
	@Column(length=300)
	private String descricao;
	
	@Override
	public void setId(Long id) {
		this.id = id;
	}

	@Override
	public Long getId() {
		return this.id;
	}

	public Compra getCompra() {
		return this.compra;
	}

	public void setCompra(Compra compra) {
		this.compra = compra;
	}

	public Date getData() {
		return data;
	}

	public void setData(Date data) {
		this.data = data;
	}

	public BigDecimal getValor() {
		return valor;
	}

	public void setValor(BigDecimal valor) {
		this.valor = valor;
	}

	public String getDescricao() {
		return descricao;
	}

	public void setDescricao(String descricao) {
		this.descricao = descricao;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		PagamentoCompra other = (PagamentoCompra) obj;
		if (id == null) {
			if (other.id != null)
				return false;
		} else if (!id.equals(other.id))
			return false;
		return true;
	}
}
